<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <script th:src="@{/layui.js}" charset="utf-8"></script>
    <link rel="stylesheet"  th:href="@{/css/layui.css}" media="all">
</head>
<body>
<br>
<form class="layui-form" action="" id="form1">
<div class="layui-form-item">
    <div class="layui-inline">
        <label class="layui-form-label">用户名</label>
        <div class="layui-input-inline">
            <input type="hidden" name="id" th:value="${user.id}">
            <input type="text" name="username" th:value="${user.username}"  autocomplete="off" class="layui-input">
        </div>
    </div>
</div>

<div class="layui-form-item">
    <div class="layui-inline">
        <label class="layui-form-label">密码</label>
        <div class="layui-input-inline">
            <input type="text" name="password" th:value="${user.password}"  autocomplete="off" class="layui-input">
        </div>
    </div>

</div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">锁住的状态</label>
            <div class="layui-input-inline">
                <select name="locked"  id="locked" lay-search="" th:value="${user.locked}">
                    <option value="0">正常</option>
                    <option value="1">锁住</option>
                </select>
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">角色</label>
            <div class="layui-input-inline">
                <input type="checkbox" name="roleIds" lay-skin="primary" th:each="role : ${roles}" th:value="${role.id}" th:title="${role.name}"
                       th:checked="${#strings.contains(user.roleNames,role.name)}">
            </div>
        </div>
    </div>
<div class="layui-form-item">
    <div class="layui-input-block">
        <button type="button" onclick="doSave();" class="layui-btn" lay-submit="" lay-filter="demo1">保存</button>
        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
    </div>
</div>
</form>


<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
<script th:inline="javascript">
    var $,layer;
    layui.use(['form'], function(){
        $ = layui.jquery;
        layer = layui.layer;

        //设置select的默认选中状态
        var locked_value = [[${user.locked}]];//取到当前页面的锁住状态的值
        var select = 'dd[lay-value=' + locked_value + ']';//属性选择器
        $('#locked').siblings("div.layui-form-select").find('dl').find(select).click();
    })
    
    
    function doSave() {
        var requestData = $("#form1").serialize();

        $.post("/user/save",requestData,function (responseData) {
            console.log(responseData);
            if(responseData.success == 1){
                layer.alert("修改成功",{yes:function () {
                    parent.layer.closeAll();
                }});
            }else{
                layer.alert("修改失败",{yes:function () {
                    parent.layer.closeAll();
                }});
            }
        });
    }
</script>


</body>
</html>